package com.umeox.moto.watch.themes.dialog;

import android.content.Context;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Space;

import com.umeox.moto.watch.themes.R;
import com.umeox.moto.watch.themes.util.MetricsUtils;
import com.umeox.moto.watch.themes.widget.ActionBar;

import java.util.ArrayList;
import java.util.List;

public class ActionDialog {
    private AlertDialog dialog;
    private AlertDialog.Builder builder;
    private View rootView;
    private List<ActionItem> items;
    private LinearLayout container;

    private ActionDialog(Context context) {
        builder = new AlertDialog.Builder(context);
        rootView = LayoutInflater.from(context).inflate(R.layout.action_dialog, null);
        container = rootView.findViewById(R.id.container);
        items = new ArrayList<ActionItem>();
    }

    public static ActionDialog build(Context context) {
        return new ActionDialog(context);
    }

    public ActionDialog create() {
        if (!items.isEmpty()) {
            for (ActionItem item : items) {
                ActionBar actionBar = new ActionBar(builder.getContext());
                actionBar.setIconRes(item.iconRes);
                if (item.titleRes != 0) {
                    actionBar.setTitleText(item.titleRes);
                } else if (item.titleText != null) {
                    actionBar.setTitleText(item.titleText);
                }
                if (item.subtitleRes != 0) {
                    actionBar.setSubtitleText(item.subtitleRes);
                } else if (item.subtitleText != null) {
                    actionBar.setSubtitleText(item.subtitleText);
                }
                int paddingVertical = (int) MetricsUtils.convertDpToPixel(builder.getContext(), 8);
                int paddingHorizontal = (int) MetricsUtils.convertDpToPixel(builder.getContext(), 44);
                actionBar.setPadding(paddingHorizontal, paddingVertical, paddingHorizontal, paddingVertical);
                if (item.clickListener != null) {
                    actionBar.setOnClickListener(v -> {
                        item.clickListener.onClick(v);
                        dismiss();
                    });
                }
                if (item.longClickListener != null) {
                    actionBar.setOnLongClickListener(v -> {
                        item.longClickListener.onLongClick(v);
                        dismiss();
                        return true;
                    });
                }
                if (item.gestureListener != null) {
                    GestureDetector detector = new GestureDetector(builder.getContext(), item.gestureListener);
                    actionBar.setOnTouchListener((v, event) -> detector.onTouchEvent(event));
                }
                container.addView(actionBar, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            }
            container.addView(new Space(builder.getContext()), new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) MetricsUtils.convertDpToPixel(builder.getContext(), 20)));
        }
        dialog = builder.setView(rootView).create();
        rootView.setOnClickListener(v -> dismiss());
        return this;
    }

    public ActionDialog addItem(int iconResId, int textResId) {
        return addItem(iconResId, textResId, null);
    }

    public ActionDialog addItem(int iconResId, int textResId, int subtitleResId) {
        return addItem(iconResId, textResId, subtitleResId, v -> {
        });
    }

    public ActionDialog addItem(int iconResId, int textResId, View.OnClickListener listener) {
        return addItem(iconResId, textResId, 0, listener);
    }

    public ActionDialog addItem(int iconResId, int titleResId, int subtitleResId, View.OnClickListener listener) {
        return addItem(new ActionItem().setIcon(iconResId).setTitleText(titleResId).setSubtitleText(subtitleResId).setOnClickListener(listener));
    }

    public ActionDialog addItem(int iconResId, int titleResId, int subtitleResId, View.OnLongClickListener listener) {
        return addItem(new ActionItem().setIcon(iconResId).setTitleText(titleResId).setSubtitleText(subtitleResId).setOnLongClickListener(listener));
    }

    public ActionDialog addItem(int iconResId, int titleResId, int subtitleResId, GestureDetector.OnGestureListener l) {
        return addItem(new ActionItem().setIcon(iconResId).setTitleText(titleResId).setSubtitleText(subtitleResId).setOnGestureListener(l));
    }

    public ActionDialog addItem(int iconRes, CharSequence title) {
        return addItem(iconRes, title, null, null);
    }

    public ActionDialog addItem(int iconRes, CharSequence title, View.OnClickListener listener) {
        return addItem(iconRes, title, null, listener);
    }

    public ActionDialog addItem(int iconRes, CharSequence title, CharSequence subtitle) {
        return addItem(iconRes, title, subtitle, null);
    }

    public ActionDialog addItem(int iconResId, CharSequence title, CharSequence subtitle, View.OnClickListener listener) {
        return addItem(new ActionItem().setIcon(iconResId).setTitleText(title).setSubtitleText(subtitle).setOnClickListener(listener));
    }

    public ActionDialog addItem(ActionItem item) {
        items.add(item);
        return this;
    }

    public void show() {
        if (dialog == null) {
            return;
        }
        dialog.show();
    }

    public void dismiss() {
        if (dialog == null) {
            return;
        }
        dialog.dismiss();
    }

    public static class ActionItem {
        private int iconRes;
        private int titleRes, subtitleRes;
        private CharSequence titleText, subtitleText;
        private View.OnClickListener clickListener;
        private View.OnLongClickListener longClickListener;
        private GestureDetector.OnGestureListener gestureListener;

        public ActionItem() {
        }

        public ActionItem setIcon(int resId) {
            this.iconRes = resId;
            return this;
        }

        public ActionItem setTitleText(int resId) {
            this.titleRes = resId;
            return this;
        }

        public ActionItem setSubtitleText(int resId) {
            this.subtitleRes = resId;
            return this;
        }

        public ActionItem setTitleText(CharSequence text) {
            this.titleText = text;
            return this;
        }

        public ActionItem setSubtitleText(CharSequence text) {
            this.subtitleText = text;
            return this;
        }

        public ActionItem setOnClickListener(View.OnClickListener l) {
            this.clickListener = l;
            return this;
        }

        public ActionItem setOnLongClickListener(View.OnLongClickListener l) {
            this.longClickListener = l;
            return this;
        }

        public ActionItem setOnGestureListener(GestureDetector.OnGestureListener l) {
            this.gestureListener = l;
            return this;
        }
    }

}
